package com.qtong.controller.assignment;

import com.qtong.entity.Result;
import com.qtong.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/assignment")
public class PurchaseController {

    @Autowired
    private TicketService ticketService;

    @Autowired
    private MongoTemplate mongoTemplate;

    public static class PurchaseRequest {
        public String screeningId;
        public List<com.qtong.dto.SeatDTO> seats;
        public String userId;
    }

    @PostMapping("/purchase")
    public Result purchase(@RequestBody PurchaseRequest req) {
        if (req == null || req.screeningId == null || req.seats == null || req.seats.isEmpty()) {
            return Result.error("请求参数不完整");
        }
        String orderId = ticketService.lockSeatsAndCreateOrder(req.screeningId, req.seats, req.userId == null ? "guest" : req.userId);
        if (orderId == null) return Result.error("锁座或创建订单失败，可能座位已被占");
        return Result.success(orderId);
    }
}
